//app.js
const baseConfig = require('./public/common/baseConfig.js')
const common = require('./public/common/common.js')
const timeStr = require('./public/common/timeStr.js')
const request = require('./public/common/request');
const login = require('./public/common/login');
const imageClassficate = require('./public/common/imageClassficate');
const order = require('./public/common/order');
const user = require('./public/common/user');
const share = require('./public/common/share');
//
App({
  onLaunch: function () {
    this.globalData.accountInfo = wx.getAccountInfoSync();
  },
  onLoad: function () {
    
  },
  onShow: function(){
    //console.log(wx.getStorageSync('userInfo_share'))
  },
  onReady: function () {

  },
  globalData: common.extend(true,
    baseConfig, //基本参数
    request,  //请求
    login,    //登录相关
    user,     //用户相关
    imageClassficate, //图片分类
    common,   //common方法
    timeStr,  //时间格式字符串处理
    order, //订单相关
    share, //海报分享
    {
    /* accountInfo  小程序账号信息  */
    accountInfo:{},
    /*  */
    //Url: 'http://192.168.103.101:11810/share',  //请求地址
    Url:'https://21h.csgxcf.com/share',
    //Url:'http://kja3dv.natappfree.cc/share',
    header:function(){
      return { //请求头
        'shareToken':this.data.userInfo_share.token||"",
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      }
    },
    contentType: 'application/x-www-form-urlencoded;charset=utf-8',
    /* 价格处理 */
    showPrice:function(price){
      let result = (price*1/100) + '';
      let reg = /\.{1}/g;
      if(reg.test(result))return result;
      return price<100?(price*1/100)+'':(price*1/100)+'.00';
    },
    /* 获取系统信息 */
    getSystemInfo:function(){
      return new Promise((resolve,reject)=>{
        wx.getSystemInfo({
          success:res=>{
            //console.log(res)
            /* 
              menuButtonTop: wx.getMenuButtonBoundingClientRect().top && wx.getMenuButtonBoundingClientRect().top, // 胶囊顶部距离
            */
            let mbInfo = wx.getMenuButtonBoundingClientRect();
            //console.log(mbInfo);
            /*  */
            getApp().globalData.windowWidth = res.windowWidth;
            getApp().globalData.windowHeight = res.windowHeight;
            //
            let hv = res.statusBarHeight + (mbInfo.top - res.statusBarHeight)*2 + mbInfo.height;
            let pt = res.statusBarHeight + (mbInfo.top - res.statusBarHeight);
            resolve({
              hv:hv,  //height
              pt:pt,  //padding-top
              ov:mbInfo.top - res.statusBarHeight
            });
          },
          fail:err=>{
            console.log(err)
            wx.showModal({
              title:'提示',
              content:'获取设备信息失败'
            });
            reject();
          }
        });
      });
    },
    /* 请求返回结果token失效 */
    check_token: function(code){
      console.log(code)
      if(code==-2){
        wx.hideLoading()
        wx.showModal({
          title:'提示',
          content:'登录token已过期，请重新授权登录！',
          showCancel: false,
          success: res=>{
            if(res.confirm){
              //清除本地信息
              wx.clearStorageSync('userInfo_share')
              //
              wx.navigateTo({
                url:'/pages/guide/guide'
              })
            }
          }
        })
        return true
      }
      return false
    },
    /* 获取缓存中的用户信息 */
    get_userInfo: function(){
      return wx.getStorageSync('userInfo_share')?wx.getStorageSync('userInfo_share'):getApp().globalData.userInfo_share;
    },
    /* 判断是否已有用户手机号码 */
    has_user_phone: function(){
      let userInfo_share = wx.getStorageSync('userInfo_share')||{}
      if(!userInfo_share.mobile){
        wx.showModal({
          title:'提示',
          content:'暂未绑定手机号码，请绑定',
          showCancel: false,
          confirmColor: app.globalData.modalConfirmColor,
          success: res=>{
            if(res.confirm){
              wx.switchTab({
                url:'/pages/personal/personal'
              })
            }
          }
        })
        return true
      }
      return false
    },
    /* preview产品图 */
    showProductImgs:function(url){
      wx.previewImage({
        urls:['' + url],
        current:'' + url
      });
    },
    /* 电话号码11位数字 */
    phoneNumberRegx:function(){
      return /^\d{11}$/g;
    },
    /* 姓名-20个汉字和“·” */
    nameRegx:function(){
      return /^([\u4e00-\u9fa5\.\。]{1,20})$/g;
    },
    /* 微信号-30个英文字母/符号/数字 */
    wechatNumberRegx:function(){
      return /^([\u4e00-\u9fa5]|[\s\uFEFF\xA0])+/g;
    },
    /* 企业名称-40个汉字 */
    companyNameRegx:function(){
      return /^([\u4e00-\u9fa5]{1,40})$/g;
    },
    /* 企业信用代码-20数字字母 */
    codeRegx:function(){
      return /^(\w{1,20})$/g;
    },
    /* Modal按钮文字颜色 */
    modalConfirmColor: '#2D8CF0'
  })
})